/*
 * Copyright (c) 2009-2011 Ricardo Quesada
 * Copyright (c) 2011-2012 Zynga Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


//
//
// Details:
//
// It uses 2 batch nodes:
//
//  * 1 batch node for the sprites
//  * 1 batch node for the platforms
//
// Why ?
// Becuase the platforms uses a different a different texture parameter
// When you using batch nodes, all the sprites MUST share the same texture and the same texture parameters
//
// TIP:
// The platforms used in this level uses the GL_REPEAT texture parameter.
// If you open the platforms.png file, you will notice the both images are 128x128. This is on purpose. You can't add more images horizontally.
// But you can add more images vertically... (only if the platforms are not higher than 128 pixels).
//
// It also uses a Parallax with 3 children:
//  - background image
//  - platforms
//  - sprites (hero, fruits, princess, etc)
//
//
// How to create a similar level ?
//	1. Open Inkscape and create a new document of 480x320. Actually it can be of any size, but it is useful as a reference.
//		-> Inkscape -> File -> Document Properties -> Custom size: width=480, height=320
//	2. Create 1 layer:
//		-> physics:objects
//	3. Start designing the world.
//
// IMPORTANT: gravity and controls are read from the svg file
//

#import "Flybike.h"
#import "BodyNode.h"
#import "Box2dDebugDrawNode.h"
#import "GameConfiguration.h"
#import "MyHero.h"

@implementation Flybike
@synthesize parallax;

-(id) init
{
	if ((self=[super init]) ) {
		
		// override box2d configuration
		
		// The car needs more iterations
		worldVelocityIterations_ = 10;
		worldPositionIterations_ = 10;
		
		
		// custom lives
		lives_ = 1;
	}
	
	return self;
}
-(void) initGraphics
{	
	// sprites
	[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sprites.plist"];

	// platforms
	[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"platforms.plist"];

	// weak ref
	spritesBatchNode_ = [CCSpriteBatchNode batchNodeWithFile:@"sprites.png" capacity:20];

	// Invisible batch node. It contains the invisible nodes
	invisibleBatchNode_ = [CCSpriteBatchNode batchNodeWithTexture:nil capacity:10];
	invisibleBatchNode_.visible = NO;		

	// weak ref
	platformBatchNode_ = [CCSpriteBatchNode batchNodeWithFile:@"platforms.png" capacity:10];
	ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
	[platformBatchNode_.texture setTexParameters:&params];
	
	//
	// TIP
	// Use 16-bit texture in background. It consumes half the memory
	//
	[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGB565];
	CCSprite *background = [CCSprite spriteWithFile:@"background1.png"];
	background.anchorPoint = ccp(0,0);

	// Restore 32-bit texture format
	[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_Default];

	//
	// Parallax Layers
	//
	parallax = [CCParallaxNode node];
	
	// background is parallaxed
	[parallax addChild:background z:-10 parallaxRatio:ccp(0.08f, 0.08f) positionOffset:ccp(-30,-30)];
    
    CCSprite *pathBg = [CCSprite spriteWithFile:@"flybike.png"];
	pathBg.anchorPoint = ccp(0,0);
//    [pathBg setPosition:ccp(-48.281, -92.868)];
//    [self addChild:pathBg z:-1];
    [parallax addChild:pathBg z:30 parallaxRatio:ccp(1, 1) positionOffset:ccp(-13.281f,6.868f)];
	
	// TIP: Disable this node in release mode
	// Box2dDebug draw in front of background
	if( [[GameConfiguration sharedConfiguration] enableWireframe] ) {
		Box2dDebugDrawNode *b2node = [Box2dDebugDrawNode nodeWithWorld:world_];
		[parallax addChild:b2node z:30 parallaxRatio:ccp(1,1) positionOffset:ccp(0,0)];
	}
	
	// batch nodes: platforms should be drawn before sprites
	[parallax addChild:platformBatchNode_ z:5 parallaxRatio:ccp(1,1) positionOffset:ccp(0,0)];
	[parallax addChild:spritesBatchNode_ z:10 parallaxRatio:ccp(1,1) positionOffset:ccp(0,0)];
	[parallax addChild:invisibleBatchNode_ z:10 parallaxRatio:ccp(1,1) positionOffset:ccp(0,0)];
    //
//    MyHero* mh= [MyHero spriteWithFile:@"Icon-72.png"];
//    [parallax addChild:mh z:10 parallaxRatio:ccp(1, 1) positionOffset:ccp(1, 1)];
//    [self addChild:mh];
    
	[self addChild:parallax];
}

- (void) dealloc
{
	[super dealloc];
}

-(CGRect) contentRect
{
	// These values were obtained from Inkscape
	return CGRectMake(-5, -60, 6890, 511);
}

-(NSString*) SVGFileName
{
	return @"flybike.svg";
}

// This is the default behavior
-(void) addBodyNode:(BodyNode*)node z:(int)zOrder
{
	switch (node.preferredParent) {
		case BN_PREFERRED_PARENT_SPRITES_PNG:
			[spritesBatchNode_ addChild:node z:zOrder];
			break;
			
		case BN_PREFERRED_PARENT_PLATFORMS_PNG:
			// Add to platform batch node
			[platformBatchNode_ addChild:node z:zOrder];
			break;

		case BN_PREFERRED_PARENT_IGNORE:
			[invisibleBatchNode_ addChild:node z:zOrder];
			break;
			
        case BN_PREFERRED_PARENT_PLATFORMS_SINGLE:
            //for sprite without of CCSpriteSheet
//            MyHero* mh= [MyHero spriteWithFile:@"Icon-72.png"];
            [parallax addChild:node z:5 parallaxRatio:ccp(1, 1) positionOffset:ccp(0, 0)];
            break;
		default:            
			NSAssert(NO, @"LevelSVG Level0#addBodyNode unsupported preferred parent");
			break;
	}
}
-(void)draw{
    
}
-(void)update:(ccTime)dt{
    [super update:dt];
}
@end